/**
 * 
 */
package com.dy.agent.agent.api.service.impl;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.stereotype.Service;

import com.dy.agent.agent.api.dal.mapper.AgentDailyViewMapper;
import com.dy.agent.agent.api.exception.BusinessException;
import com.dy.agent.agent.api.exception.CommonErrorCode;
import com.dy.agent.agent.api.model.entiry.AgentDailyView;
import com.dy.agent.agent.api.service.AgentDailyViewService;
import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @author leiruiqi
 *
 */
@Service
public class AgentDailyViewServiceImpl implements AgentDailyViewService {

	@Resource
	private AgentDailyViewMapper agentDailyViewMapper;

	@Override
	public void batchCreate(Collection<AgentDailyView> views) {
		for (AgentDailyView view : views) {
			/*
			 * try { agentDailyViewMapper.insertSelective(view); } catch
			 * (Exception e) { // TODO: handle exception }
			 */
			agentDailyViewMapper.insertSelective(view);
		}

	}

	@Override
	public AgentDailyView getByGidAndDt(Long gid, String dt) {
		AgentDailyView view = new AgentDailyView();
		view.setGid(gid);
		view.setDt(dt);
		return agentDailyViewMapper.selectOne(view);
	}

	@Override
	public AgentDailyView getTodayByGid(Long gid) {
		DateTimeFormatter dayformat = DateTimeFormat.forPattern("yyyyMMdd");
		DateTime now = DateTime.now();
		String last1DayStr = now.plusDays(-1).toString(dayformat);
		return getByGidAndDt(gid, last1DayStr);
	}

	@Override
	public AgentDailyView getLastViewByGid(Long gid) {

		PageHelper.startPage(1, 1, false);
		Example condition = new Example(AgentDailyView.class);
		condition.orderBy("dt").desc();
		condition.createCriteria().andEqualTo("gid", gid);
		List<AgentDailyView> list = agentDailyViewMapper.selectByExample(condition);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			DateTimeFormatter dayformat = DateTimeFormat.forPattern("yyyyMMdd");
			DateTime now = DateTime.now();
			String last1DayStr = now.plusDays(-1).toString(dayformat);
			return new AgentDailyView(gid, last1DayStr);
		}
	}

	@Override
	public List<AgentDailyView> listByIdsAndDt(Collection<Long> gids, String dt) {
		if (CollectionUtils.isEmpty(gids) && StringUtils.isBlank(dt)) {
			throw new BusinessException(CommonErrorCode.REQUIRE_PARAM, "gids and dt can not null");
		}
		Example condition = new Example(AgentDailyView.class);
		Criteria criteria = condition.createCriteria();
		if (CollectionUtils.isNotEmpty(gids)) {
			criteria.andIn("gid", gids);
		}
		if (CollectionUtils.isNotEmpty(gids)) {
			condition.orderBy("dt").desc();
		}

		List<AgentDailyView> list = agentDailyViewMapper.selectByExample(condition);
		return list;
	}

	@Override
	public int delByIdsAndDt(Collection<Long> gids, String dt) {
		if (CollectionUtils.isEmpty(gids) && StringUtils.isBlank(dt)) {
			throw new BusinessException(CommonErrorCode.REQUIRE_PARAM, "gids and dt can not null");
		}
		Example condition = new Example(AgentDailyView.class);
		Criteria criteria = condition.createCriteria();
		if (CollectionUtils.isNotEmpty(gids)) {
			criteria.andIn("gid", gids);
		}
		if (CollectionUtils.isNotEmpty(gids)) {
			condition.orderBy("dt").desc();
		}

		return agentDailyViewMapper.deleteByExample(condition);
	}

}
